﻿Public Class frmCalorieCounter

    Private _FoodPlanList As FoodPlanList = Nothing
    Private _loadedDt As Date
    Private _loadingList As Boolean = False

    Public ReadOnly Property FoodPlanList As FoodPlanList
        Get
            If _FoodPlanList Is Nothing Then
                _FoodPlanList = New FoodPlanList()
            End If

            Return _FoodPlanList
        End Get
    End Property
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        LoadFoodPlan(dteFoodPlanDay.Value)
    End Sub

    ''' <summary>
    ''' Called to add a food to the day
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdAddFood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddFood.Click
        Dim item As New FoodPlanEntry()

        Try
            With item
                .FoodName = txtFoodName.Text
                .EatenFor = txtEatenFor.Text
                .ApproxCalories = System.Convert.ToInt32(txtCalories.Text)
                .FatContent = System.Convert.ToInt32(txtFat.Text)
                .FoodPlanDate = dteFoodPlanDay.Text
            End With

            FoodPlanList.Add(item)
            FoodPlanList.SaveByDate(_loadedDt)

            LoadFoodPlan(_loadedDt)
        Catch ex As Exception
            MessageBox.Show(ex.ToString(), "Calorie Counter")
        End Try


    End Sub

    ''' <summary>
    ''' Called to load the datagridview for the specified date
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdLoadDay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadDay.Click
        Try
            LoadFoodPlan(dteFoodPlanDay.Value)
        Catch ex As Exception
            MessageBox.Show(ex.ToString(), "Calorie Counter")
        End Try
    End Sub

    ''' <summary>
    ''' Loads the Food Plan datagridview for the specified date
    ''' </summary>
    ''' <param name="dt"></param>
    ''' <remarks></remarks>
    Public Sub LoadFoodPlan(ByVal dt As Date)
        Dim totalCalories As Integer
        Dim totalFat As Integer

        _loadingList = True


        Me.FoodPlanList.LoadByDate(dt)

        _loadedDt = dt

        Me.dgvFoodPlan.DataSource = Nothing
        Me.dgvFoodPlan.DataSource = Me.FoodPlanList

        totalCalories = 0
        totalFat = 0

        For Each item2 As FoodPlanEntry In FoodPlanList
            totalCalories = totalCalories + item2.ApproxCalories
            totalFat = totalFat + item2.FatContent

        Next

        txtTotalCalories.Text = System.Convert.ToString(totalCalories)
        txtTotalFat.Text = System.Convert.ToString(totalFat)

        Me.Refresh()

        _loadingList = False
    End Sub
    ''' <summary>
    ''' Modifies the selected food plan entry using the information from the Add/Modify food section
    ''' </summary>
    Private Sub cmdModifyFood_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModifyFood.Click
        Try
            Dim entry As FoodPlanEntry = GetSelectedObject()
            If (entry IsNot Nothing) Then
                With entry
                    .FoodName = txtFoodName.Text
                    .EatenFor = txtEatenFor.Text
                    .ApproxCalories = System.Convert.ToInt32(txtCalories.Text)
                    .FatContent = System.Convert.ToInt32(txtFat.Text)
                    .FoodPlanDate = dteFoodPlanDay.Value
                End With

                If (dgvFoodPlan.SelectedRows.Count > 0) Then
                    With Me.FoodPlanList(dgvFoodPlan.SelectedRows(0).Index)

                        .FoodName = entry.FoodName
                        .EatenFor = entry.EatenFor
                        .ApproxCalories = entry.ApproxCalories
                        .FatContent = entry.FatContent
                        .FoodPlanDate = entry.FoodPlanDate

                    End With

                    FoodPlanList.SaveByDate(_loadedDt)

                    LoadFoodPlan(_loadedDt)

                End If


            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString(), "Calorie Counter")
        End Try
    End Sub

    ''' <summary>
    ''' Called when a new food plan entry is selected
    ''' </summary>
    Private Sub dgvFoodPlan_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFoodPlan.RowEnter
        Try
            If (_loadingList) Then
                Exit Sub
            End If
            Dim entry As FoodPlanEntry = GetSelectedObject()
            If entry IsNot Nothing Then


                With entry
                    txtFoodName.Text = .FoodName
                    txtEatenFor.Text = .EatenFor
                    txtCalories.Text = System.Convert.ToString(.ApproxCalories)
                    txtFat.Text = System.Convert.ToString(.FatContent)
                End With

            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString(), "Calorie Counter")
        End Try
    End Sub

    ''' <summary>
    ''' Utility function returning the currently selected FoodPlanEntry
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetSelectedObject() As FoodPlanEntry
        If (dgvFoodPlan.SelectedRows.Count > 0) Then
            Return TryCast(dgvFoodPlan.SelectedRows(0).DataBoundItem, FoodPlanEntry)
        Else
            Throw New Exception("No item selected")
        End If
        Return Nothing
    End Function

    ''' <summary>
    ''' Called to exit the application
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdExitApplication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExitApplication.Click
        Application.Exit()
    End Sub
End Class
